package com.egao.api.controller.orderB;


import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.common.system.service.OperRecordService;
import com.egao.orderB.entity.Customer;
import com.egao.orderB.entity.CustomerCopy;
import com.egao.orderB.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * api登录相关接口
 * Created by wangfan on 2019-05-29 下午 3:43.
 * http://localhost/swagger-ui.html
 * https://francjc.com:4332/swagger-ui.html
 *
 */
@RequestMapping("/api/customer")
@RestController
@Api(value = "货代",tags = {"货代"})
public class ApiCustomerController extends BaseController {

    @Autowired
    private OperRecordService operRecordService;
    @Autowired
    private CustomerService customerService;

    @PreAuthorize("hasAuthority('orderB:customer:list')")
    @PostMapping("/list")
    @ApiOperation(value = "货代列表",notes = "limit:每页显示几行 必填\n" +
            "page:第几页 必填\n" +
            "name  货代名称\n" +
            "abbreviation  货代名称缩写\n" +
            "type 货代类型:车队,货代,报关行,订舱代理\n" +
            "state  状态:未审核/已审核\n" +
            "salesman  业务员\n" +
            "operater  操作\n" +
            "settle  周结,月结\n" +
            "返回的数据:id int id\n" +
            "organizationId varchar 组织id\n" +
            "name varchar 货代名称\n" +
            "profilePicture;//头像   放到司机专用的文件夹registerDriver\n" +
            "adminUser varchar 管理员帐号\n" +
            "code varchar 货代代码\n" +
            "email varchar email\n" +
            "socialCode varchar 统一社会信用代码\n" +
            "businessLicense varchar 营业执照图片\n" +
            "roadTransportPermit 道路运输许可证\n" +
            "agreement 合同\n" +
            "agreementStart;//合同开始时间\n" +
            "agreementEnd;//合同结束时间\n" +
            "settle varchar 结算周期(提醒用的)周结,月结\n" +
//            "contactsId varchar 联系人id\n" +
            "contacts varchar 联系人\n" +
            "contactsPhone varchar 联系人电话\n" +
            "salesmanId;业务员id\n" +
            "salesman;业务员\n" +
            "operaterId;操作id\n" +
            "operater;操作\n" +
            "address varchar 组织店址\n" +
            "invoiceState 是否开票:开票1 不开票0\n" +
            "type 货代类型:车队,货代,报关行,订舱代理\n" +
            "state 审核状态: 未审核,已审核\n" +
            "message 是否发送短信 1发送 0不发送\n"+
            "gzhSend 是否发送公众号 1发送 0不发送\n"+
            "orderNum int 排序\n" +
            "offerLimitNum 客户下单数量限制\n" +
            "createTime datetime 创建时间\n" +
            "createUser varchar 创建人\n" +
            "updateTime datetime 更新时间\n" +
            "updateUser varchar 更新人\n" +
            "deleted int del(0正常 1删除)",httpMethod = "POST")
    public JsonResult list(PageResult2<T> page, Customer customer, HttpServletRequest request)  throws Exception{
        return customerService.list(page, customer, getLoginUser());
    }

    @PreAuthorize("hasAuthority('orderB:customer:list')")
    @PostMapping("/listAll")
    @ApiOperation(value = "货代列表",notes = "" +
            "name  货代名称\n" +
            "abbreviation  货代名称缩写\n" +
            "type 货代类型:车队,货代,报关行,订舱代理\n" +
            "state  状态:未审核/已审核\n" +
            "salesman  业务员(我们组织的)\n" +
            "settle  周结,月结\n\n\n\n\n\n" +
            "返回的数据:id int id\n" +
            "organizationId varchar 组织id\n" +
            "name varchar 货代名称\n" +
            "profilePicture;//头像   放到司机专用的文件夹registerDriver\n" +
            "adminUser varchar 管理员帐号\n" +
            "code varchar 货代代码\n" +
            "email varchar email\n" +
            "socialCode varchar 统一社会信用代码\n" +
            "businessLicense varchar 营业执照图片\n" +
            "roadTransportPermit 道路运输许可证\n" +
            "agreement 合同\n" +
            "agreementStart;//合同开始时间\n" +
            "agreementEnd;//合同结束时间\n" +
            "settle varchar 结算周期(提醒用的)周结,月结\n" +
//            "contactsId  联系人id\n" +
            "contacts varchar 联系人\n" +
            "contactsPhone varchar 联系人电话\n" +
            "salesmanId;业务员id\n" +
            "salesman;业务员\n" +
            "operaterId;操作id\n" +
            "operater;操作\n" +
            "address varchar 组织店址\n" +
            "invoiceState 是否开票:开票1 不开票0\n" +
            "type 货代类型:车队,货代,报关行,订舱代理\n" +
            "state varchar 审核状态: 未审核,已审核\n" +
            "message 是否发送短信 1发送 0不发送\n"+
            "orderNum int 排序\n" +
            "offerLimitNum 客户下单数量限制\n" +
            "createTime datetime 创建时间\n" +
            "createUser varchar 创建人\n" +
            "updateTime datetime 更新时间\n" +
            "updateUser varchar 更新人\n" +
            "deleted int del(0正常 1删除)",httpMethod = "POST")
    public JsonResult listAll(Customer customer, HttpServletRequest request)  throws Exception{
        return customerService.list(customer, getLoginUser());
    }

    @PreAuthorize("hasAuthority('orderB:customer:list')")
    @PostMapping("/getById")
    @ApiOperation(value = "货代查询",notes = "id ",httpMethod = "POST")
    public JsonResult getById(Customer customer, HttpServletRequest request) {
        return JsonResult.ok().setData(customerService.getById(customer));
    }

    @PreAuthorize("hasAuthority('orderB:customer:list')")
    @PostMapping("/getByName")
    @ApiOperation(value = "货代查询 根据名称查询",notes = "name ",httpMethod = "POST")
    public JsonResult getByName(Customer customer, HttpServletRequest request) {
        return customerService.getByName(customer);
    }

    @PreAuthorize("hasAuthority('orderB:customer:save')")
    @OperLog(value = "货代添加", desc = "添加", result = true)
    @PostMapping("/save")
    @ApiOperation(value = "平台(货代,车队)添加",notes = "" +
            "username 帐号 必填\n" +
            "password 密码 必填\n" +
            "phone 电话 必填\n" +
            "type 货代类型:车队,货代,报关行,订舱代理 必填\n" +
            "name  货代名称 必填\n" +
            "abbreviation  货代名称缩写\n" +
            "profilePicture;//头像   放到司机专用的文件夹registerDriver\n" +
            "business_license  营业执照图片地址\n" +
            "roadTransportPermit 道路运输许可证(车队才有的)\n" +
            "agreement 合同(车队才有的)\n" +
            "agreementStart;//合同开始时间\n" +
            "agreementEnd;//合同结束时间\n" +
//            "contactsId  联系人id\n" +
            "contacts  联系人\n" +
            "contactsPhone  联系人电话\n" +
            "salesmanId  业务员id 有业务员的时候需要传id\n" +
            "salesman  业务员\n" +
            "operaterId  操作id  有操作的时候需要传id\n" +
            "operater  操作\n" +
            "address  地址\n" +
            "settle  结算周期(提醒用的)周结,月结 \n" +
            "state 审核状态: 未审核,已审核 \n" +
            "offerLimitNum 客户下单数量限制\n" +
            "",httpMethod = "POST")
    public JsonResult save(CustomerCopy customer, User user, HttpServletRequest request) {
        try{
            return customerService.ApiSave(customer, new User(UserConstant.ADMIN_ID,UserConstant.ORGANIZATION_ID), user);
        } catch (Exception e) {
            return JsonResult.error("注册失败: 用户名已被注册");
        }
    }

    @PreAuthorize("hasAuthority('orderB:customer:update')")
    @OperLog(value = "货代修改", desc = "修改", result = true)
    @PostMapping("/update")
    @ApiOperation(value = "货代修改",notes = "" +
            "id id 必填\n" +
            "code varchar 组织代码\n" +
            "profilePicture;//头像   放到司机专用的文件夹registerDriver\n" +
            "agreementStart;//合同开始时间\n" +
            "agreementEnd;//合同结束时间\n" +
            "email varchar email\n" +
            "contacts varchar 联系人\n" +
            "contactsPhone varchar 联系人电话\n" +
            "salesmanId  业务员id 有业务员的时候需要传id\n" +
            "salesman  业务员\n" +
            "operaterId  操作id  有操作的时候需要传id\n" +
            "operater  操作\n" +
            "address varchar 组织地址\n" +
            "settle varchar 结算周期(提醒用的)周结,月结\n" +
            "offerLimitNum 客户下单数量限制\n" +
            "",httpMethod = "POST")
    public JsonResult update(Customer customer, HttpServletRequest request) {
        return customerService.update(customer, getLoginUser());
    }

    @PreAuthorize("hasAuthority('orderB:customer:update')")
    @OperLog(value = "设置货代是否开票", desc = "设置货代是否开票", result = true)
    @PostMapping("/updateInvoiceState")
    @ApiOperation(value = "设置货代是否开票",notes = "" +
            "id id 必填\n" +
            "invoiceState 是否开票:开票1 不开票0\n" +
            "",httpMethod = "POST")
    public JsonResult updateInvoiceState(Customer customer, HttpServletRequest request) {
        return customerService.updateInvoiceState(customer, getLoginUser());
    }

    @PreAuthorize("hasAuthority('orderB:customer:update')")
    @OperLog(value = "是否发送短信给货代", desc = "是否发送短信给货代", result = true)
    @PostMapping("/updateMessage")
    @ApiOperation(value = "是否发送短信给货代",notes =
            "id id 必填\n" +
            "message 是否发送短信 1发送 0不发送\n",
            httpMethod = "POST")
    public JsonResult updateMessage(Customer customer, HttpServletRequest request) {
        return customerService.updateMessage(customer, getLoginUser());
    }

    @PreAuthorize("hasAuthority('orderB:customer:update')")
    @OperLog(value = "是否推送公众号消息给货代", desc = "是否发送短信给货代", result = true)
    @PostMapping("/updateGzhSend")
    @ApiOperation(value = "是否发送短信给货代",notes =
            "id id 必填\n" +
            "gzhSend 是否发送公众号 1发送 0不发送\n",
            httpMethod = "POST")
    public JsonResult updateGzhSend(Customer customer, HttpServletRequest request) {
        return customerService.updateGzhSend(customer, getLoginUser());
    }

    @PreAuthorize("hasAuthority('orderB:customer:update')")
    @OperLog(value = "货代审核通过", desc = "货代审核通过", result = true)
    @PostMapping("/examine")
    @ApiOperation(value = "货代审核通过",notes = "id id",httpMethod = "POST")
    public JsonResult examineSuccess(Customer customer, HttpServletRequest request) {
        return customerService.examine(customer, getLoginUser());
    }


    @PreAuthorize("hasAuthority('orderB:customer:update')")
    @OperLog(value = "货代修改", desc = "货代审核未通过", result = true)
    @PostMapping("/examineFail")
    @ApiOperation(value = "货代审核未通过",notes = "id id",httpMethod = "POST")
    public JsonResult examineFail(Customer customer, HttpServletRequest request) {
        customer.setState("未通过");
        return customerService.examineFail(customer, getLoginUser());
    }

    @PreAuthorize("hasAuthority('orderB:customer:update')")
    @OperLog(value = "货代删除,账号会一起删除", desc = "货代删除", result = true)
    @PostMapping("/remove")

    @ApiOperation(value = "货代删除",notes =
            "id id 必填\n" + "",
            httpMethod = "POST")
    public JsonResult remove(Customer customer, HttpServletRequest request) {
        return customerService.remove(customer, getLoginUser());
    }

}
